// pages/release/release.js 
const app = getApp()
const db = wx.cloud.database();//初始化数据库
Page({
  /**
    * 页面的初始数据
    */
  data: {
    checkboxArr: [
      {
        checked: false,//是否选中
        id: "1",//风格能id
        name: "时尚",
      },
      {
        checked: false,//是否选中
        id: "2",//风格能id
        name: "古风",
      },
      {
        checked: false,//是否选中
        id: "3",//风格能id
        name: "潮流",
      },
      {
        checked: false,//是否选中
        id: "4",//风格能id
        name: "运动",
      },
      {
        checked: false,//是否选中
        id: "5",//部门能id
        name: "休闲",
      },
      {
        checked: false,//是否选中
        id: "6",//风格能id
        name: "甜美",
      },
      {
        checked: false,//是否选中
        id: "7",//风格能id
        name: "韩风",
      },
      {
        checked: false,//是否选中
        id: "8",//风格能id
        name: "朋克",
      },
    ],//风格列表

   
    show:false,
    checkValue: [],//风格
    depValue: [],//风格
    isDep: false,//部门显隐
    titles:"",//标题
    texts:"",//正文
    imgbox: [],//选择图片
    fileIDs: [],//上传云存储后的返回值
    avatarUrl: 'images/wx.png',
    userInfo: {},
    checkValue2:[],
    checked:[],
       

    // 添加商品
    UserId:[],
    Collection:[],
    title:[],
    img:[],
    rmb:[],
    UserId2:[],
    Collection2:[],
    title2:[],
    img2:[],
    rmb2:[]
  },
  show:function(){
    var that=this
    that.setData({
      show:!that.data.show
    })
  },

  //控制风格的显隐
  isDep: function () {
    this.setData({
      isDep: true,
    })
  },


  //取消按钮
  cancel: function () {
    this.setData({
      isDep: false,
      isPer: false
    })
  },

  

  //风格
  checkbox: function (e) {   
    var index = e.currentTarget.dataset.index;//获取当前点击的下标
    var checkboxArr = this.data.checkboxArr;//选项集合
    checkboxArr[index].checked = !checkboxArr[index].checked;//改变当前选中的checked值

    this.setData({
      checkboxArr: checkboxArr
    });
  },
  checkboxChange: function (e) {
    var checkValue = e.detail.value;
    this.setData({
      checkValue: checkValue
    });
  },
  confirm: function () {// 确定
    this.setData({
      isDep: false,
      // depValue: []如果是联动，选中部门后需要清除选中的成员
    })
    // this.getUser();//通过选中部门来获取不通的部门成员，将获取到的数据存入checkboxArr中
  },
  delectCheckValue2:function(e){
    console.log(e.currentTarget.id)
    var checkValue=this.data.checkValue2
    var checked=this.data.checked
    checked[checkValue[e.currentTarget.id]]=false
    checkValue.splice(e.currentTarget.id,1)
    console.log(checkValue[e.currentTarget.id])
    this.setData({
      checkValue2:checkValue,
      checked:checked
     });
     console.log(this.data.checked)
  },
  checked:function(e){
    console.log(e.currentTarget.id)
    var checked=this.data.checked
    checked[e.currentTarget.id]=!checked[e.currentTarget.id],
    this.setData({
      checked:checked
     });
  },
  checkboxChange2: function (e) {
    var checkValue = e.detail.value;
    this.setData({
     checkValue2: checkValue,
    });
    console.log(e.detail.value)
  },

  onLoad: function () {
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      var openid=res.result.openid
      db.collection('User').where({
        _openid:openid
      }).get().then(res => {
        // res.data 包含该记录的数据
        console.log(res.data[0].type)
        var c= this.data.checked
        for(let i=0;i<res.data[0].Collection.length;i++){
          c[i]=false
          this.setData({
            checked:c
          })     
        }
        this.setData({
          UserId:res.data[0]._id,
          Collection:res.data[0].Collection,
          title:res.data[0].title,
          img:res.data[0].img,
          rmb:res.data[0].rmb
        })
        console.log(this.data.img)
      })
    }).catch(err => {
      console.log(err);
    })
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              console.log(res.userInfo.nickName)
              console.log(res.userInfo.avatarUrl)
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  // 删除照片 &&
  imgDelete1: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.deindex;
    let imgbox = this.data.imgbox;
    imgbox.splice(index, 1)
    that.setData({
      imgbox: imgbox
    });
  },
  // 选择图片 &&&
  addPic1: function (e) {
    var imgbox = this.data.imgbox;
    console.log(imgbox)
    var that = this;
    var n = 5;
    if (5 > imgbox.length > 0) {
      n = 5 - imgbox.length;
    } else if (imgbox.length == 5) {
      n = 1;
    }
    wx.chooseImage({
      count: n, // 默认9，设置图片张数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res.tempFilePaths)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        if (imgbox.length == 0) {
          imgbox = tempFilePaths
        } else if (5 > imgbox.length) {
          imgbox = imgbox.concat(tempFilePaths);
        }
        that.setData({
          imgbox: imgbox
        });
      }
    })
  },

  //图片
  imgbox: function (e) {
    this.setData({
      imgbox: e.detail.value
    })
  },
  bt: function (e) {
    console.log(e.detail.value)
    this.setData({
      titles: e.detail.value
    })
  },
  zw: function (e) {
    console.log(e.detail.value)
    this.setData({
     texts: e.detail.value
    })
  },

  //发布按钮
  fb: function (e) {
    if (!this.data.imgbox.length) {
      wx.showToast({
        icon: 'none',
        title: '图片类容为空'
      });
    } else {
      //上传图片到云存储
      wx.showLoading({
        title: '上传中',
      })
      let promiseArr = [];
      for (let i = 0; i < this.data.imgbox.length; i++) {
        promiseArr.push(new Promise((reslove, reject) => {
          let item = this.data.imgbox[i];
          let suffix = /\.\w+$/.exec(item)[0];//正则表达式返回文件的扩展名
          wx.cloud.uploadFile({
            cloudPath: new Date().getTime() + suffix, // 上传至云端的路径
            filePath: item, // 小程序临时文件路径
            success: res => {
              this.setData({
                fileIDs: this.data.fileIDs.concat(res.fileID)
              });
              
              console.log(res.fileID)//输出上传后图片的返回地址
              reslove();
              
            },
            fail: res => {
              wx.hideLoading();
              wx.showToast({
                title: "上传失败",
              })
            }

          })
        }));
      }
      Promise.all(promiseArr).then(res => {//等数组都做完后做then方法
        console.log("图片上传完成后再执行")
        var g=this.data.checkValue2
        console.log(g)
        var UserId=this.data.UserId
        var Collection=this.data.Collection
        var Collection2=this.data.Collection2
        var title=this.data.title
        var img=this.data.img
        var rmb=this.data.rmb
        var Collection2=this.data.Collection2
        var title2=this.data.title2
        var img2=this.data.img2
        var rmb2=this.data.rmb2
        console.log(Collection[g[0]])
        for(let i=0;i<g.length;i++){
          Collection2=Collection2.concat(Collection[g[i]])
          title2=title2.concat(title[g[i]])
          img2=img2.concat(img[g[i]])
          rmb2=rmb2.concat(rmb[g[i]])
        }
        console.log(Collection2)
        db.collection('Bowman').add({
          data: {
            title: this.data.titles,
            text: this.data.texts,
            imageurl: this.data.fileIDs,
            zan: 0,
            dressingStyle: this.data.checkValue,
            nickName: this.data.userInfo.nickName,
            avatarUrl: this.data.userInfo.avatarUrl,
            GoodsId:Collection2,
            GoodsTitle:title2,
            GoodsRmb:rmb2,
            GoodsImg:img2
          },
          success: function (res) {
            console.log(res)
            wx.hideLoading();
              wx.showToast({
              title: "上传成功",
            })
          }
        })
        this.setData({
          imgbox: []
        })
      })
    }
  },

})